import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;

import java.io.IOException;


public class ValueFilterDemo {
    static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://192.168.126.139:9000/hbase");
        conf.set("hbase.master", "hdfs://192.168.126.139:60010");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
    }

    public static void main(String[] args) throws IOException, IllegalAccessException {
        HTable table = new HTable(conf, "student");
        table.setAutoFlush(false);
        Scan sc = new Scan();
        Filter vf = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("zhangsan"));
        sc.setFilter(vf);
        ResultScanner scanner = table.getScanner(sc);
        for (Result res : scanner) {
            for (Cell cell : res.rawCells()) {
                System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");
                System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");
                System.out.print("value=" + new String(CellUtil.cloneValue(cell)) + ",");
                System.out.println("timestamp=" + cell.getTimestamp());
            }
            System.out.println("---------------------------------------------------");
        }
        scanner.close();
        table.close();

    }
}
